package com.lt.flashcard.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.A;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Paging;
import org.zkoss.zul.Window;
import org.zkoss.zul.event.PagingEvent;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.TaskQueue;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.TaskService;

public class V_AdminFlashcardSetListCtrl extends SelectorComposer<Component> {

	private static final long serialVersionUID = 1L;
	
	private List<FlashcardSet> flashcardsets;
	
	private AdminProductManagerCtrl ctrl = null;
	
	private int curpage;
	private int pagesize;
	private long total;
	
	@Wire
	private	Paging pager;

	/* (non-Javadoc)
	 * @see org.zkoss.zk.ui.select.SelectorComposer#doBeforeComposeChildren(org.zkoss.zk.ui.Component)
	 */
	@Override
	public void doBeforeComposeChildren(Component comp) throws Exception {
		Component com = Path.getComponent("//admin_product_manager/ctnAdminProductManager");
		ctrl = (AdminProductManagerCtrl) com.getAttribute("$composer");
		flashcardsets = ctrl.getFlashcardsets();
		setCurpage(ctrl.getCurpage());
		setPagesize(ctrl.getPagesize());
		total = ctrl.getTotal();
		super.doBeforeComposeChildren(comp);
	}
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		getPager().setActivePage(curpage);
	}
	
	@Listen("onClick=.btnDelete")
	public void deleteFlashcard(Event event) {
		A a = (A) event.getTarget();
		final Long id = (Long) a.getAttribute("id");
		Messagebox.show("Are you sure?", "Confirm", Messagebox.OK | Messagebox.NO, Messagebox.QUESTION, new EventListener<Event>() {
			public void onEvent(Event e) throws Exception {
				if(e.getName().equals("onOK")) {
					FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
					FlashcardSet fcs = srv.findByID(id);
					if(fcs != null) {
						if(fcs.getAdmin() == FlashcardSet.ADMIN_YES)
							Messagebox.show("Can't delete blueup set");
						else if(fcs.getAdmin() == FlashcardSet.ADMIN_BUY)
							Messagebox.show("Can't delete bought FCS");
						else
							try {
								srv.delete(fcs);
								ctrl.notifyChangePage(curpage);
								Messagebox.show("Finish");
							}
							catch (Exception ex) {
								Messagebox.show(ex.getMessage());
							}
					}
				}
			}
		});
	}
	
	@Listen("onPaging=#pager")
	public void paging(PagingEvent event) {
		ctrl.notifyChangePage(event.getActivePage());
	}
	
	@Listen("onClick=.btnDeleteDetail")
	public void deleteDetail(Event event) {
		A a = (A) event.getTarget();
		final Long id = (Long) a.getAttribute("id");
		if(id != null) {
			Messagebox.show("Are you sure?", "Confirm", Messagebox.OK | Messagebox.NO, Messagebox.QUESTION, new EventListener<Event>() {
				
				public void onEvent(Event e) throws Exception {
					if(e.getName().equals("onOK")) {
						FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
						FlashcardSet fcs = srv.findByID(id);
						if(fcs != null) {
							fcs.removeAllFlashcards();
							srv.save(fcs);
						}
						
						ctrl.loadAdminList();
						Messagebox.show("Finish");
					}
				}
			});
		}
	}
	
	private Long updateId = null;
	@Listen("onClick=.btnUpdate")
	public void updateFlashcardset(Event event) {
		A a = (A) event.getTarget();
		final Long id = (Long) a.getAttribute("id");
		if(id != null) {
			updateId = id;
			FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("count", srv.countNumberOfSoldProducts(id));
			param.put("parent", this);
			Window window = (Window)Executions.createComponents("/admin/product_update.zul", null, param);
		    window.doModal();
		}
	}
	
	public void doScheduleUpdateFlashcardSet(boolean setimage, boolean setdetail, boolean cardimage, boolean carddetail) {
		TaskService srv = Repository.getInstance().getTaskService();
		srv.addTaskUpdateFlashcardSet(updateId, setimage, setdetail, cardimage, carddetail);
		Messagebox.show("Plan is scheduled. You will be notified when finish.");
		Utility.redirect("", true);
	}

	/**
	 * @return the flashcardsets
	 */
	public List<FlashcardSet> getFlashcardsets() {
		return flashcardsets;
	}

	/**
	 * @param flashcardsets the flashcardsets to set
	 */
	public void setFlashcardsets(List<FlashcardSet> flashcardsets) {
		this.flashcardsets = flashcardsets;
	}
	
	public String getProductCategory(FlashcardSet fcs) {
		return fcs.getProductcategory().getName();
	}
	
	public String getAuthor(FlashcardSet fcs) {
		return fcs.getAuthor().getUsername();
	}
	
	public int getTaskStatus(Long fcsid) {
		TaskService tsr = Repository.getInstance().getTaskService();
		TaskQueue rs = tsr.getTaskUpdateFlashcardSet(fcsid);
		if(rs == null)
			return -1;
		else
			return rs.getStatus();
	}

	/**
	 * @return the curpage
	 */
	public int getCurpage() {
		return curpage;
	}

	/**
	 * @param curpage the curpage to set
	 */
	public void setCurpage(int curpage) {
		this.curpage = curpage;
	}

	/**
	 * @return the pagesize
	 */
	public int getPagesize() {
		return pagesize;
	}

	/**
	 * @param pagesize the pagesize to set
	 */
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}

	/**
	 * @return the total
	 */
	public long getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(long total) {
		this.total = total;
	}

	/**
	 * @return the pager
	 */
	public Paging getPager() {
		return pager;
	}

	/**
	 * @param pager the pager to set
	 */
	public void setPager(Paging pager) {
		this.pager = pager;
	}
}
